﻿using System.Text;


namespace Teal.Compiler {

    /// <summary>
    /// 内部工具函数集合。
    /// </summary>
    static class Utility {

        public static bool isUnicode(string value) {
            for (int i = 0; i < value.Length; i++) {
                if (value[i] >= 256) {
                    return true;
                }
            }

            return false;

        }

        /// <summary>
        /// 将指定内容以字符串形式表示。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string escapeString(string value) {
            StringBuilder sb = new StringBuilder(value.Length + 2);
            sb.Append('\"');
            for (int i = 0; i < value.Length; i++) {
                switch (value[i]) {
                    case '\'':
                    case '\"':
                    case '\\':
                        sb.Append('\\');
                        sb.Append(value[i]);
                        break;
                    case '\n':
                        sb.Append('\\');
                        sb.Append('n');
                        break;
                    case '\r':
                        sb.Append('\\');
                        sb.Append('r');
                        break;
                    case '\t':
                        sb.Append('\\');
                        sb.Append('t');
                        break;
                    case '\0':
                        sb.Append('\\');
                        sb.Append('0');
                        break;
                    case '\a':
                        sb.Append('\\');
                        sb.Append('a');
                        break;
                    case '\b':
                        sb.Append('\\');
                        sb.Append('b');
                        break;
                    case '\f':
                        sb.Append('\\');
                        sb.Append('f');
                        break;
                    case '\v':
                        sb.Append('\\');
                        sb.Append('v');
                        break;
                    default:
                        sb.Append(value[i]);
                        break;
                }
            }
            sb.Append('\"');

            return sb.ToString();
        }

        /// <summary>
        /// 创建指定可变字符串的副本。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static StringBuilder clone(StringBuilder value) {
            var result = new StringBuilder(value.Length);
            for (var i = 0; i < value.Length; i++) {
                result.Append(value[i]);
            }
            return result;
        }

        //internal static void log(ISymbol members) {
        //    if (members is MethodGroupReference) {
        //        log((MethodGroupReference)members);
        //    } else {
        //        Compiler.warning(-1, ((MemberReference)members).fullName);
        //    }
        //}
        //internal static void log(MethodGroupReference members) {
        //    foreach (var item in members) {
        //        Compiler.warning(-1, item.fullName);
        //    }
        //}

        //internal static void trace(string message) {
        //    Compiler.warning(9, message);
        //}
    }

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
    struct SingleConverter {
        [System.Runtime.InteropServices.FieldOffset(0)]
        int i;

#pragma warning disable 414
        [System.Runtime.InteropServices.FieldOffset(0)]
        float f;
#pragma warning restore 414

        public static int SingleToInt32Bits(float v) {
            SingleConverter c = new SingleConverter();
            c.f = v;
            return c.i;
        }
    }

}